package com.inspur.jxpcxt;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.inspur.jxpcxt.activitys.HomeActivity;
import com.inspur.jxpcxt.activitys.TeacherCommentActivity;
import com.inspur.jxpcxt.app.JXPCApplication;
import com.inspur.jxpcxt.bean.EvaInfo;
import com.inspur.jxpcxt.bean.StudentCourse;
import com.inspur.jxpcxt.bean.TeaInfo;
import com.inspur.jxpcxt.bean.TeaRankinginfo;
import com.inspur.jxpcxt.bean.TeacherEvaInfo;
import com.inspur.jxpcxt.service.LoginService;
import com.inspur.jxpcxt.service.TeacherCommentsService;
import com.inspur.jxpcxt.utils.JXPCLog;
import com.inspur.jxpcxt.utils.NetworkUtils;
import com.inspur.jypcxt.R;

/**
 * 实现登录界面
 * 
 * @author xiang de shan
 * 
 */
public class LoginActivity extends Activity implements OnClickListener {
	// 用户名
	private EditText mUserIdEdt = null;
	// 密码
	private EditText mUserPasswordEdt = null;
	// 登录按钮
	private Button mLoginBtn = null;
	// 不能登录
	private TextView mNotLoginTv = null;
	// 新用户注册
	private TextView mNewUserTv = null;
	// 是否是教员登陆
	CheckBox mTeaLoginCk = null;

	ArrayList<StudentCourse> studentCourses = new ArrayList<StudentCourse>();
	TeacherEvaInfo evaInfos;

	List<EvaInfo> evaInfoList = new ArrayList<EvaInfo>();
	List<TeaRankinginfo> rankingList = new ArrayList<TeaRankinginfo>();
	
	List<TeaInfo> teaInfoslist = new ArrayList<TeaInfo>();
	// ArrayList<EvaInfo> arrayList = (ArrayList<EvaInfo>)
	// evaInfos.get(0).getTeaevainfos();

	@Override
	protected void onCreate(Bundle savedInstanceState)
		{
			super.onCreate(savedInstanceState);
			// requestWindowFeature(Window.FEATURE_NO_TITLE);
			setContentView(R.layout.activity_main);
			JXPCApplication.getInstance().addActivity(LoginActivity.this);
			initUI();
			initData();
		}

	/**
	 * 初始化UI
	 */
	private void initUI()
		{
			mUserIdEdt = (EditText) findViewById(R.id.edt_login_name);
			mUserPasswordEdt = (EditText) findViewById(R.id.edt_login_password);
			mLoginBtn = (Button) findViewById(R.id.btn_login);
			mNotLoginTv = (TextView) findViewById(R.id.tv_can_not_login);
			mNewUserTv = (TextView) findViewById(R.id.tv_new_user);
			mTeaLoginCk = (CheckBox) findViewById(R.id.cb_teacher_login);

			mLoginBtn.setOnClickListener(this);
			mNewUserTv.setOnClickListener(this);
			mNotLoginTv.setOnClickListener(this);
		}

	private void initData()
		{

		}

	@Override
	public void onClick(View v)
		{
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.btn_login:
				if (mTeaLoginCk.isChecked()) {
					// if(mUserIdEdt.getText().toString().equals("123") &&
					// mUserPasswordEdt.getText().toString().equals("456")){
					// 登陆到教师界面
					loginTeacher();
					// }
				} else {
					// 登录
					loginStudent();
				}
				break;
			case R.id.tv_can_not_login: {
				JXPCLog.e("tv_can_not_login");
				Intent intent = new Intent();
				intent.setData(Uri.parse("xds://www.xiangdeshan.com:7777/xds"));
				startActivity(intent);
			}
				break;
			case R.id.tv_new_user: {
				Intent intent = new Intent();
				intent.setData(Uri.parse("xds://www.xiangdeshan.com:8888/xds"));
				startActivity(intent);
			}
				break;

			default:
				break;
			}
		}

	/**
	 * 登录函数
	 */
	private void loginStudent()
		{
			final String sno = mUserIdEdt.getText().toString().trim();
			final String pswd = mUserPasswordEdt.getText().toString().trim();
			if (TextUtils.isEmpty(sno) || TextUtils.isEmpty(pswd)) {
				Toast.makeText(LoginActivity.this, "学号或密码为空", Toast.LENGTH_LONG)
						.show();
				return;
			}
			if (!NetworkUtils.isConnectInternet(this)) {
				Toast.makeText(LoginActivity.this, "网络不可用，请检查网络连接",
						Toast.LENGTH_SHORT).show();
				return;
			}
			// new AsyncTask<Params, Progress, Result>() {
			// }.execute();
			new Thread() {
				public void run()
					{
						// 调用NewUserService里面的方法请求网络获取数据
						final String result = LoginService.LoginByClientGet(
								sno, pswd);
						JXPCLog.e("zoudaozhele" + result.length());
						if (!result.equals("登录失败") && result.length() > 0) {
							runOnUiThread(new Runnable() {
								@SuppressWarnings({ "unchecked", "rawtypes" })
								public void run()
									{
										Gson gson = new Gson();
										studentCourses = gson
												.fromJson(
														result,
														new TypeToken<ArrayList<StudentCourse>>() {
														}.getType());
										// for (StudentCourse sc :
										// studentCourses) {
										// JXPCLog.e("sssss",sc.toString());
										// }

										Bundle data = new Bundle();
										data.putParcelableArrayList("sc",
												(ArrayList) studentCourses);
										Intent intent = new Intent(
												LoginActivity.this,
												HomeActivity.class);
										intent.putExtras(data);
										startActivity(intent);
										LoginActivity.this.finish();
									}
							});
						} else {
							// 请求失败
							runOnUiThread(new Runnable() {
								public void run()
									{
										// 请求失败时有dialog提示
										Toast.makeText(LoginActivity.this,
												"请求服务失败。。。", Toast.LENGTH_LONG)
												.show();
										mUserIdEdt.setText("");
										mUserPasswordEdt.setText("");
										return;
									}
							});
						}
					}
			}.start();
		}

	
	private void loginTeacher()
		{
			final String tno = mUserIdEdt.getText().toString().trim();
			final String tpswd = mUserPasswordEdt.getText().toString().trim();
			if (TextUtils.isEmpty(tno) || TextUtils.isEmpty(tpswd)) {
				Toast.makeText(LoginActivity.this, "账号或密码为空", Toast.LENGTH_LONG).show();
			}
			if (!NetworkUtils.isConnectInternet(this)) {
				Toast.makeText(LoginActivity.this, "网络不可用，请检查网络连接",
						Toast.LENGTH_SHORT).show();
				return;
			}
			// TODO Auto-generated method stub

			// 子线程访问网络
			new AsyncTask<Void, Void, String>() {

				@Override
				protected String doInBackground(Void... params)
					{
						// TODO Auto-generated method stub
						String result = TeacherCommentsService.TeaByClientGet(tno, tpswd);
						// if (result == null) {
						// return "";
						// }
						JXPCLog.e("teadasdaf", result.toString());
						return result;
					}

				@SuppressWarnings({ "unchecked", "rawtypes" })
				@Override
				protected void onPostExecute(String result)
					{
						if(result.equals("登录失败")){
							Toast.makeText(LoginActivity.this, "请核实你的密码和账号！", Toast.LENGTH_LONG).show();
							return;
						}
						if(result.length() == 0){
							Toast.makeText(LoginActivity.this, "请求失败……", Toast.LENGTH_LONG).show();
							return;
						}
						// 解析数据
						Gson gson = new Gson();
						teaInfoslist = gson.fromJson(result,
								new TypeToken<ArrayList<TeaInfo>>() {
								}.getType());
						
						if (teaInfoslist == null) {
							return;
						}
//						 for (EvaInfo ei : arrayList) {
//						 JXPCLog.e("sssss", ei.toString());
//						 }
						Bundle data = new Bundle();
						data.putParcelableArrayList("teainfos", (ArrayList)teaInfoslist);
						data.putString("tno", tno);
//						Intent intent = new Intent(LoginActivity.this,
//								TeacherActivity.class);
						Intent intent = new Intent(LoginActivity.this,TeacherCommentActivity.class);
						//intent.setDataAndType(Uri.parse("xds://www.xiangdeshan.com:8888/xds"),"abc/test");
						intent.putExtras(data);
						JXPCLog.e("loginteacher");
						startActivity(intent);
						LoginActivity.this.finish();
					}
			}.execute();

					}
}
